package com.sgb.kjwl.viewmodel.transport;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.self.ProprietaryDriverEntity;
import com.sgb.kjwl.model.response.ListResponseV1;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LogisticsEvent;
import com.sgb.net.util.RequestUtil;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.dialog.RoundDialog;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class DriverBindViewModel extends BaseViewModel {
    public MutableLiveData<ListResponseV1<List<ProprietaryDriverEntity>>> ldList = new MutableLiveData<>();
    private int mPage = 1;
    public String mDriverName;

    public DriverBindViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    /**
     * 获取司机列表
     */
    public void getDriverList(Context context, boolean isRefresh) {
        if (isRefresh) {
            mPage = 1;
        }
        RequestUtil requestUtil = RequestUtil.map();
        if (!TextUtils.isEmpty(mDriverName)) {
            requestUtil.add("driverName", mDriverName);
        }
        requestUtil.add("limit", 15);
        requestUtil.add("page", mPage);
        DialogHelper.showProgressDialog(context, "加载中");
        NewAPIManager.getInstance().getLogisticsAPI().getDriverList(requestUtil.buildMap())
                .enqueue(new ICallBack<BaseEntity<ListResponseV1<List<ProprietaryDriverEntity>>>>() {
                    @Override
                    public void onSuccess(BaseEntity<ListResponseV1<List<ProprietaryDriverEntity>>> data) {
                        if (!data.isSuccess()) {
                            ldList.postValue(new ListResponseV1<>(false));
                            return;
                        }
                        ldList.postValue(data.getData());
                        if (data.getData().haveMoreData()) {
                            mPage = data.getData().nextPage();
                        }
                    }

                    @Override
                    public void onFail() {
                        ldList.postValue(new ListResponseV1<>(false));
                    }
                });
    }

    /**
     * 绑定司机
     */
    public void bindDriver(Context context, long no, ProprietaryDriverEntity entity) {
        RequestUtil requestUtil = RequestUtil.map();
        requestUtil.add("driverNo", entity.getDriverNo());
        requestUtil.add("name", entity.getName());
        requestUtil.add("telphone", entity.getTelphone());
        requestUtil.add("no", no);
        DialogHelper.showProgressDialog(context, "加载中");
        NewAPIManager.getInstance().getLogisticsAPI().bindDriver(requestUtil.buildMap())
                .enqueue(new ICallBack<BaseEntity>() {
                    @Override
                    public void onSuccess(BaseEntity data) {
                        EventBus.getDefault().post(LogisticsEvent.make().setMessageTag(EventConstant.EVENT_BIND_DRIVER));
                        showSuccessDialog(context);
                    }
                });
    }

    private void showSuccessDialog(Context context) {
        new RoundDialog.Builder(context)
                .title("提示")
                .content("绑定成功")
                .contentGravity(Gravity.CENTER)
                .singleBtn(true)
                .cancelable(false)
                .confirmCallback(v -> ((Activity) context).finish())
                .show();
    }
}
